package com.elock.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.WebUtils;

/**
 * Dao - 任务信息
 * 
 * @author 仁德
 */
@Repository("terminalTaskDao")
public class TerminalTaskDao extends DaoCenter {

	public Page<Map<String, Object>> findPage(Pageable pageable, Object[] args) {

		List<Object> list = new ArrayList<Object>();
		Long companyInfoId = WebUtils.getCurrentCompanyInfoId();
		String waybillNo = (String) args[0];
		Long categoryId = (Long) args[1];
		Long fromSaleOrgId = (Long) args[2];
		Integer status = (Integer) args[3];
		Long toSaleOrgId = (Long) args[4];

		StringBuilder sql = new StringBuilder();
		sql.append(
				"select t.*,ct.value category_value,so.name from_sale_org_name,tso.name to_sale_org_name");
		sql.append(" from terminal_task t");
		sql.append(" left join system_dict ct on ct.id=t.category");
		sql.append(" left join sale_org so on so.id=t.from_sale_org");
		sql.append(" left join sale_org tso on tso.id=t.to_sale_org");
		sql.append(" where t.company_info_id=?");
		list.add(companyInfoId);

		if (!ConvertUtil.isEmpty(waybillNo)) {
			sql.append(" and t.waybill_no like ?");
			list.add("%" + waybillNo + "%");
		}
		if (categoryId != null) {
			sql.append(" and t.category = ?");
			list.add(categoryId);
		}
		if (fromSaleOrgId != null) {
			sql.append(" and t.from_sale_org = ?");
			list.add(fromSaleOrgId);
		}
		if (toSaleOrgId != null) {
			sql.append(" and t.to_sale_org = ?");
			list.add(toSaleOrgId);
		}
		if (status != null) {
			sql.append(" and t.status = ?");
			list.add(status);
		}
		sql.append(" order by t.create_date desc");

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		Page<Map<String, Object>> page = getNativeDao().findPageMap(sql.toString(),
				objs,
				pageable);

		return page;
	}

	public List<Map<String, Object>> findItemsByTaskId(String taskIds) {

		StringBuilder sql = new StringBuilder();
		sql.append("select ti.*,so.name to_sale_org_name");
		sql.append(" from terminal_task_item ti");
		sql.append(" left join sale_org so on so.id=ti.to_sale_org");
		sql.append(" where ti.terminal_task in (" + taskIds + ")");

		List<Map<String, Object>> items = getNativeDao().findListMap(sql.toString(),
				null,
				0);

		return items;
	}

	public String findWaybillNo(String plateNum, Long neId) {

		List<Object> list = new ArrayList<Object>();
		Long companyInfoId = WebUtils.getCurrentCompanyInfoId();

		StringBuilder sql = new StringBuilder();
		sql.append("select t.waybill_no from terminal_task_item ti,terminal_task t");
		sql.append(" where ti.terminal_task=t.id and t.status=0");
		sql.append(" and t.company_info_id=? and ti.plate_num=?");
		list.add(companyInfoId);
		list.add(plateNum);
		if (neId != null) {
			sql.append(" and t.id<>?");
			list.add(neId);
		}
		sql.append(" limit 1");

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		String waybillNo = getNativeDao().findString(sql.toString(), objs);
		return waybillNo;
	}

	public void complete(String ids) {

		StringBuilder sql = new StringBuilder();
		sql.append("update terminal_task set status=1 where status=0 and id in ("
				+ ids
				+ ")");
		getNativeDao().update(sql.toString(), null);
	}

	public String findWorkingItem(String plateNum, Integer berth, Long id) {
		List<Object> list = new ArrayList<Object>();
		Long companyInfoId = WebUtils.getCurrentCompanyInfoId();
		StringBuilder sql = new StringBuilder();
		sql.append(
				"select waybill_no from terminal_task where company_info_id=? and plate_num=? and berth=?");
		sql.append(" and status=0 and end_time>? and start_time<=?");
		list.add(companyInfoId);
		list.add(plateNum);
		list.add(berth);
		list.add(new Date());
		list.add(new Date());
		if (id != null) {
			sql.append(" and id<>?");
			list.add(id);
		}
		sql.append(" limit 1");
		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		String waybillNo = getNativeDao().findString(sql.toString(), objs);
		return waybillNo;
	}
}